package com.zj.play.home

import androidx.lifecycle.ViewModelProvider
import com.youth.banner.indicator.CircleIndicator
import com.zj.core.almanac.isZhLanguage
import com.zj.play.R
import com.zj.play.article.ArticleAdapter
import com.zj.play.home.almanac.AlmanacActivity
import com.zj.play.home.search.SearchActivity
import com.zj.play.main.MainActivity
import kotlinx.android.synthetic.main.fragment_home_page.*


class HomePageFragment : ArticleCollectBaseFragment() {

    private val viewModel by lazy { ViewModelProvider(this).get(HomePageViewModel::class.java) }

    override fun getLayoutId(): Int = R.layout.fragment_home_page

    override fun onResume() {
        super.onResume()
        homeBanner.start()
        homeBanner2.start()
    }

    override fun refreshData() {
        getArticleList(true)
    }

    private lateinit var bannerAdapter: ImageAdapter
    private lateinit var bannerAdapter2: ImageAdapter
    private lateinit var articleAdapter: ArticleAdapter
    private var page = 1

    override fun initView() {
        homeTitleBar.setRightText(getString(R.string.search))
        if (isZhLanguage()) {
            homeTitleBar.setTitleOnClickListener {
                AlmanacActivity.actionStart(context!!)
            }
        }
        homeTitleBar.setRightTextOnClickListener {
            SearchActivity.actionStart(context!!)
        }
        bannerAdapter = ImageAdapter(context!!, viewModel.bannerList)
        bannerAdapter2 = ImageAdapter(context!!, viewModel.bannerList2)
        homeBanner.adapter = bannerAdapter
        homeBanner.setIndicator(CircleIndicator(context)).start()
        homeBanner2.adapter = bannerAdapter2
        homeBanner2.setIndicator(CircleIndicator(context)).start()
        homeToTopRecyclerView.setRecyclerViewLayoutManager(true)
        articleAdapter = ArticleAdapter(context!!, viewModel.articleList)
        homeToTopRecyclerView.onRefreshListener({
            page = 1
            getArticleList(true)
        }, {
            page++
            getArticleList(true)
        })
        homeToTopRecyclerView.setAdapter(articleAdapter)
    }

    override fun initData() {
        startLoading()
        initBanner()
        setDataStatus(viewModel.articleLiveData, {
            if (viewModel.articleList.size > 0) loadFinished()
        }) {
            if (page == 1 && viewModel.articleList.size > 0) {
                viewModel.articleList.clear()
            }
            viewModel.articleList.addAll(it)
            articleAdapter.notifyDataSetChanged()
        }
        getArticleList(false)
    }

    private fun initBanner() {
        setDataStatus(viewModel.getBanner(), {
            if (viewModel.bannerList.size > 0) loadFinished()
        }) {
            val main = activity as MainActivity
            if (viewModel.bannerList.size > 0)
                viewModel.bannerList.clear()
            if (viewModel.bannerList2.size > 0)
                viewModel.bannerList2.clear()
            if (main.isPort) {
                viewModel.bannerList.addAll(it)
            } else {
                for (index in it.indices) {
                    if (index / 2 == 0) {
                        viewModel.bannerList.add(it[index])
                    } else {
                        viewModel.bannerList2.add(it[index])
                    }
                }
            }
            bannerAdapter.notifyDataSetChanged()
            bannerAdapter2.notifyDataSetChanged()
        }

    }

    private fun getBanner() {
        viewModel.getBanner()
    }

    private fun getArticleList(isRefresh: Boolean) {
        viewModel.getArticleList(page, isRefresh)
    }

    override fun onPause() {
        super.onPause()
        homeBanner.stop()
        homeBanner2.stop()
    }

    companion object {
        @JvmStatic
        fun newInstance() = HomePageFragment()
    }

}